import logging
logging.basicConfig(level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[logging.StreamHandler()])
import os
from PySide6.QtCore import QUrl, Qt, QTimer
from PySide6.QtWidgets import QMessageBox
from PySide6.QtMultimedia import QMediaPlayer
from PySide6.QtGui import QPixmap
from show_lyrics import LyricsManager


def handle_playlist_double_click(item, playlist_widget, media_player, lyrics_list, music_info, artist_name, album_name, bit_rate, audio_format, cover_label):
    if item:
        file_path = item.data(6, Qt.ItemDataRole.UserRole)
        meta = item.data(6, Qt.ItemDataRole.UserRole + 1)
        
        # 更新歌曲信息显示
        music_info.setTitle(meta['title'])
        artist_name.setText(meta['artist'])
        album_name.setText(meta['album'])
        bit_rate.setText(meta['bitrate'])
        audio_format.setText(meta['audioformat'])

        # 更新专辑封面
        if meta.get('cover_data'):
            cover_pixmap = QPixmap()
            cover_pixmap.loadFromData(meta['cover_data'])
            cover_label.setPixmap(cover_pixmap.scaled(71, 71, Qt.AspectRatioMode.KeepAspectRatio, Qt.SmoothTransformation))
        else:
            cover_label.setPixmap(QPixmap("icons/default_cover.png"))

        #重要，提供文件路径给媒体播放器
        media_player.setSource(QUrl.fromLocalFile(file_path))
            
        media_player.play()
        playlist_widget.play_button.setText("暂停")
        # 加载歌词
        playlist_widget.lyrics_manager.load_lyrics(file_path)

    else:
        QMessageBox.warning(playlist_widget, '错误', '未选中任何项目')
    
